package cn.bocweb.spinning.view.forun;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 16/3/15.
 */
public class ForunDetailAdapter extends MyAdapter<ForumDetailBean.CommentBean> {

    public ForunDetailAdapter(Context context, List<ForumDetailBean.CommentBean> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_forun_detail, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(parent.getContext());

        if (!TextUtils.isEmpty(list.get(position).getReceiverId()) && list.get(position).getReceiverId().equals(dataEntity.getUserid())) {
            holder.forunDetailReceiver.setVisibility(View.VISIBLE);
            holder.forunDetailReceiver.setText(new StringBuffer("-->  ").append(list.get(position).getReceiver()));
        } else {
            holder.forunDetailReceiver.setVisibility(View.GONE);
        }

        holder.textviewLocation.setText(list.get(position).getLocation());

        Glide
                .with(parent.getContext())
                .load(list.get(position).getUthumb())
                .placeholder(R.mipmap.login_head)
                .transform(new GlideCircle(parent.getContext()))
                .into(holder.forunDetailHead);

        holder.forunDetailContent.setText(list.get(position).getContent());
        holder.forunDetailName.setText(list.get(position).getNickname());
        holder.forunDetailTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(list.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.forun_detail_head)
        ImageView forunDetailHead;
        @Bind(R.id.forun_detail_name)
        TextView forunDetailName;
        @Bind(R.id.forun_detail_content)
        TextView forunDetailContent;
        @Bind(R.id.forun_detail_reply)
        TextView forunDetailReply;
        @Bind(R.id.forun_detail_time)
        TextView forunDetailTime;
        @Bind(R.id.forun_detail_receiver)
        TextView forunDetailReceiver;
        @Bind(R.id.textview_location)
        TextView textviewLocation;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
